package com.shop.shoporder.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.StringUtil;
import com.shop.shoporder.entity.GroupbuyGood;
import com.shop.shoporder.entity.GroupbuyGoodInfo;
import com.shop.shoporder.service.GroupbuyGoodService;
import com.shop.shoporder.service.GroupbuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/groupbuy")
public class AfterGroupbuyController {

    @Autowired
    GroupbuyService groupbuyService;
    @Autowired
    GroupbuyGoodService groupbuyGoodService;

    /**
     * 团购商品的增加、修改、删除的方法
     *
     * @param request 参数需要增加、修改、删除的参数
     * @return 返回操作的对象
     */
    @ResponseBody
    @RequestMapping(value = "/addGroupbuyGood", method = RequestMethod.POST)
    public Response addGroupbuyGood(HttpServletRequest request) {
        String type = null;
        type = request.getParameter("type");
        if (type == null) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, "", request);
        }

        int groupbuygoodId = 0;

        int goodsId = 0;

        int specId = 0;

        double groupbyPrice = 0.0;

        Date startTime = null;

        Date endTime = null;

        int goodsCount = 0;

        String groupbuyStatu = null;

        int groupbuySort = 0;

        double buyonePrice = 0.0;
        int classfyId = 0;

        String groupbuygoodIds = request.getParameter("groupbuygoodId");
        String goodsIds = request.getParameter("goodsId");
        String specIds = request.getParameter("specId");
        String groupbyPrices = request.getParameter("groupbyPrice");
        String startTimes = request.getParameter("startTime");
        String endTimes = request.getParameter("endTime");
        String goodsCounts = request.getParameter("goodsCount");
        groupbuyStatu = request.getParameter("groupbuyStatu");
        String groupbuySorts = request.getParameter("groupbuySort");
        String buyonePrices = request.getParameter("buyonePrice");
        String classfyIds = request.getParameter("classfyId");


        if (groupbuygoodIds != null && !"".equals(groupbuygoodIds)) {
            groupbuygoodId = Integer.parseInt(groupbuygoodIds);
        }
        if (goodsIds != null && !"".equals(goodsIds)) {
            goodsId = Integer.parseInt(goodsIds);
        }

        if (specIds != null && !"".equals(specIds)) {
            specId = Integer.parseInt(specIds);
        }

        if (groupbyPrices != null && !"".equals(groupbyPrices)) {
            groupbyPrice = Double.parseDouble(groupbyPrices);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {

            if (startTimes != null && !"".equals(startTimes)) {
                startTime = sdf.parse(startTimes);
            }

            if (endTimes != null && !"".equals(endTimes)) {
                endTime = sdf.parse(endTimes);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        if (goodsCounts != null && !"".equals(goodsCounts)) {
            goodsCount = Integer.parseInt(goodsCounts);
        }

        if (groupbuySorts != null && !"".equals(groupbuySorts)) {
            groupbuySort = Integer.parseInt(groupbuySorts);
        }

        if (classfyIds != null && !"".equals(classfyIds)) {
            classfyId = Integer.parseInt(classfyIds);
        }

        if (buyonePrices != null && !"".equals(buyonePrices)) {
            buyonePrice = Double.parseDouble(buyonePrices);
        }


        GroupbuyGood groupbuyGood = new GroupbuyGood();
        groupbuyGood.setGroupbuygoodId(groupbuygoodId);
        groupbuyGood.setGoodsId(goodsId);
        groupbuyGood.setSpecId(specId);
        groupbuyGood.setGroupbyPrice(groupbyPrice);
        groupbuyGood.setStartTime(startTime);
        groupbuyGood.setEndTime(endTime);
        groupbuyGood.setGoodsCount(goodsCount);
        groupbuyGood.setGroupbuyStatu(groupbuyStatu);
        groupbuyGood.setBuyonePrice(buyonePrice);
        groupbuyGood.setClassfyId(classfyId);
        groupbuyGood.setGroupbuySort(groupbuySort);
        //type:1为增加 2为修改，3为删除
        int row = 0;
        switch (type) {
            //增加
            case "1":
                row = groupbuyGoodService.insertSelective(groupbuyGood);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, groupbuyGood, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SYS_ERROR, null, "", request);
                }

                //修改
            case "2":
                row = groupbuyGoodService.updateByPrimaryKeySelective(groupbuyGood);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, groupbuyGood, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SYS_ERROR, null, "", request);
                }

                //删除
            case "3":
                row = groupbuyGoodService.deleteByPrimaryKey(groupbuygoodId);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, groupbuyGood, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SYS_ERROR, null, "", request);
                }


            default:
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, "", request);


        }


    }


    /**
     * 查询团购产品列表
     * @param request 参数 以及分类ID
     * @return 返回团购商品列表
     */
    @ResponseBody
    @RequestMapping("/getGroupbuyGoodList")
    public Response getGroupbuyGoodList(HttpServletRequest request){

        String classfyId = request.getParameter("classfyId");
        int classfyIds = 0;

        int page = 1;
        int limit = 5;
        String pages = request.getParameter("page");
        String limits = request.getParameter("limit");
        if (pages != null && pages != "") {
            page = Integer.parseInt(pages);
        }
        if (limits != null && limits != "") {
            limit = Integer.parseInt(limits);
        }
        List<GroupbuyGoodInfo> groupbuyGoodInfoList = null;
        PageInfo<GroupbuyGoodInfo> pageInfo = null;

        if (classfyId == null ||  "".equals(classfyId) || !StringUtil.isNumeric(classfyId)) {

            //调用列表
            PageHelper.startPage(page, limit);
            groupbuyGoodInfoList = groupbuyGoodService.getGroupbuyGooods();
            pageInfo = new PageInfo<>(groupbuyGoodInfoList);
        } else {
            //根据分类ID 查询
            classfyIds = Integer.parseInt(classfyId);
            PageHelper.startPage(page, limit);
            groupbuyGoodInfoList = groupbuyGoodService.getGroupbuyGoodByClassfyId(classfyIds);
            pageInfo = new PageInfo<>(groupbuyGoodInfoList);
        }

        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
    }






}
